package org.cttic.visualization.visualization.z_service.impl;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.cttic.visualization.visualization.common.EsDataIncomplete;
import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.z_service.ElasticsearchOptionServer;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-07 14:01
 */
@Service
public class ElasticsearchOptionServerImpl implements ElasticsearchOptionServer {
    @Autowired
    private EmployeeRepository employeeRepository;


//    @Autowired
//    private TransportClient transportClient;

    /**
     * 多个条件查询
     * 使用QueryBuilder
     * termQuery("key", obj) 完全匹配
     * termsQuery("key", obj1, obj2..)   一次匹配多个值
     * matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
     * multiMatchQuery("text", "field1", "field2"..);  匹配多个字段, field有通配符忒行
     * matchAllQuery();     匹配所有文件
     *
     * @param map
     * @return
     */

    @Override
    public List<EsDataStrip> getByMoreName(Map<String, String> map) {


        List<EsDataStrip> strips = employeeRepository.findByVehicleNoAndVehicleColor(map.get("vehicleNo"),map.get("vehicleColor"));
        for (EsDataStrip esDataStrip : strips) {
            System.out.println("AND:"+JSONUtil.toJsonStr(esDataStrip));
        }
//        List<EsDataStrip> timeAfter = employeeRepository.findByVehicleNoAndVehicleColorAndTansTimeAfter(map.get("vehicleNo"), map.get("vehicleColor"), map.get("startTime"));
//        for (EsDataStrip esDataStrip : timeAfter) {
//            System.out.println("After:"+JSONUtil.toJsonStr(esDataStrip));
//        }
//
//        List<EsDataStrip> timeBetween = employeeRepository.findByVehicleNoAndVehicleColorAndTansTimeBetween(map.get("vehicleNo"), map.get("vehicleColor"), map.get("startTime"),map.get("endTime"));
//        for (EsDataStrip esDataStrip : timeBetween) {
//            System.out.println("BETWEEN:"+JSONUtil.toJsonStr(esDataStrip));
//        }

        return strips;
    }

    @Override
    public List<EsDataIncomplete> getByMoreNameIncomplete(Map<String, String> map) {
        List<EsDataIncomplete> timeBetween2 = employeeRepository.findByVehicleNoAndTansTimeBetweenAndVehicleColor(map.get("vehicleNo"), map.get("startTime"),map.get("endTime"), map.get("vehicleColor"));
        for (EsDataIncomplete esDataStrip : timeBetween2) {
            System.out.println("Incomplete,BETWEEN"+JSONUtil.toJsonStr(esDataStrip));
        }
        return timeBetween2;
    }
}
